package com.xkstream.client.ui;

import android.os.Bundle;
import android.support.annotation.UiThread;
import android.support.v7.widget.DefaultItemAnimator;
import android.view.View;

import com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView;
import com.xkstream.client.R;
import com.xkstream.client.application.Application;
import com.xkstream.client.base.BaseActivity;
import com.xkstream.client.base.BaseData;
import com.xkstream.client.entity.User;
import com.xkstream.client.entity.kt.MessagesData;
import com.xkstream.client.presenter.kt.MessageContract;
import com.xkstream.client.presenter.kt.MessagePresenter;
import com.xkstream.client.ui.adapter.MessageAdapter;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by xiaoyi on 2017-9-26.
 */

public class MessageActivity extends BaseActivity<MessageContract.Presenter>
        implements MessageContract.View {
    @BindView(R.id.refresh)
    PullLoadMoreRecyclerView refreshView;
    MessageAdapter adapter;
    List<MessagesData.Message> mDatas = new ArrayList<>();

    private int pageSize = 10;
    private int page = 1;

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_message);
        ButterKnife.bind(this);
        setTitle(R.string.message);
        this.presenter = new MessagePresenter(this);
        initRefreshView();
        getMessageList();


    }

    private void initRefreshView() {
        refreshView.setLinearLayout();
        adapter = new MessageAdapter(this, mDatas);
        refreshView.setAdapter(adapter);
        refreshView.setItemAnimator(new DefaultItemAnimator());


        refreshView.setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getMessageList();
            }

            @Override
            public void onLoadMore() {
                page++;
                getMessageList();
            }
        });

        //testData();


    }

    @OnClick({R.id.headerBack})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.headerBack:
                finish();
                break;
        }
    }


    private void getMessageList() {
        User user = Application.getUser();
        Map<String, String> params = new HashMap<>();
        params.put("user_code", user.getUser_code());
        params.put("cur_page", String.valueOf(page));
        params.put("page_size", String.valueOf(pageSize));
        presenter.getMessageList(params);
    }

    @Override
    public void showMessageList(BaseData<MessagesData> data) {
        refreshView.setPullLoadMoreCompleted();
        if (data.getData() != null) {
            readMessages(data.getData().getList());
            if (page == 1) {
                mDatas.clear();
                mDatas.addAll(data.getData().getList());
                adapter.notifyDataSetChanged();
            } else {
                mDatas.addAll(data.getData().getList());
                adapter.notifyDataSetChanged();
            }
        }

    }

    private void readMessages(List<MessagesData.Message> datas) {

        for (int i = 0; i < datas.size(); i++) {
            if (datas.get(i).getStatus() == 1) continue;
            Map<String, String> params = new HashMap<>();
            params.put("id", datas.get(i).getId());
            presenter.readMessage(params);
        }

    }

    @Override
    public void setPresenter(MessageContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @UiThread
    @Override
    public void showError(@NotNull String message) {
        showTopTips(message);
    }

    @Override
    public void hideLoading() {
        super.hideLoading();
        refreshView.setPullLoadMoreCompleted();
    }
}
